package com.omp.dao.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.springside.modules.orm.Page;

import com.omp.common.dao.BaseDAO;
import com.omp.dao.UserLevelDAO;
import com.omp.entity.UserLevel;

@Repository
public class UserLevelDAOImpl extends BaseDAO<UserLevel, Integer> implements  UserLevelDAO{

	@Override
	public Page<UserLevel> findByPage(Page<UserLevel> page, UserLevel userLevel) {
		StringBuilder hql = new StringBuilder("from UserLevel where 1=1");
		Map<String, Object> params = new HashMap<String, Object>();
		if (userLevel != null) {
			if (StringUtils.isNotBlank(userLevel.getLevelName())) {
				hql.append(" and name like :name");
				params.put("name", "%" + userLevel.getLevelName() + "%");
			}
		}
		return findPage(page, hql.toString(), params);
	}

	@Override
	public boolean nameExists(String name) {
		String hql = "select count(id) from UserLevel where levelName=?";
		return ((Long)findUnique(hql, name)) > 0;
	}

	@Override
	public void deleteById(Integer id) {
		String hql = "delete UserLevel where levelId=?";
		batchExecute(hql, id);
	}

}
